home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Snippets / Files / PBDTGetAppl / Source / EventLoop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-07  |  6.0 KB  |  258 lines  |  [TEXT/KAHL]

  1. /*
  2.     9-30-92  • Brigham Stevens
  3.     --------------------------
  4.     This file contains all the code needed to handle dispatching events.
  5.     Any files that need to access the globals should include the EventLoop.h file.
  6.     
  7.     This is a very basic event loop that does not take into account any
  8.     real "document" type of windows.  It just does the minimum default Mac type
  9.     behavior for windows and menus.  Many parts of the code could be changed
  10.     to include a case on window type, and then dispatch to a handler for that type
  11.     of window.
  12. */
  13.  
  14. #include "EventLoop.h"
  15.  
  16. /* These Globals are here in case any other tasks need access to the latest key presssed */
  17. /* I use this for games */
  18.  
  19. Boolean        Done = false;                /* true when Quit is selected */
  20. Boolean        KeyPressed = false;            /* true most recent event is a keyDown event */
  21. char        KeyValue = 0;                /* the ascii code of the most recent key down */
  22.  
  23. Boolean        WNE_available = false;        /* Does WNE exist on this mac?? */
  24. Boolean        BackgroundFlag = false;        /* true if we are in the background */
  25.  
  26. void DoCloseWindow(register EventRecord *evt, register WindowPtr theWindow)
  27. {
  28.     if(TrackGoAway(theWindow,evt->where)) {
  29.         CloseWindow(theWindow);
  30.     }
  31. }
  32.  
  33. void DoClickInContent(register EventRecord *evt, register WindowPtr theWindow)
  34. {
  35.     int                part;
  36.     ControlHandle    ctlh;
  37.     Point            pt;
  38.     GrafPtr            saveport;
  39.     Rect            frame;
  40.     
  41.     if(theWindow!=FrontWindow()) {
  42.         SelectWindow(theWindow);
  43.     } else {
  44.         GetPort(&saveport);
  45.         SetPort(theWindow);
  46.         pt = evt->where;
  47.         GlobalToLocal(&pt);
  48.         if(part = FindControl(pt,theWindow,&ctlh)) {
  49.             /* TrackControl Goes Here */
  50.         }
  51.         SetPort(saveport);
  52.     }
  53. }
  54.  
  55.  
  56.  
  57.  
  58. void DoDragWindow(register EventRecord *evt, register WindowPtr theWindow)
  59. {
  60.     DragWindow(theWindow,evt->where,&screenBits.bounds);
  61. }
  62.  
  63.  
  64. void DoGrowWindow(register EventRecord *evt, register WindowPtr theWindow)
  65. {
  66.     long    newSize;
  67.     int        newHeight,newWidth;
  68.     Rect    growLimitSizes;
  69.     
  70.     SetPort(theWindow);
  71.     InvalRect(&theWindow->portRect);
  72.     
  73.     growLimitSizes.top = 20;            /* min height */
  74.     growLimitSizes.bottom = 32767;        /* max height */
  75.     growLimitSizes.left = 20;             /* min width */
  76.     growLimitSizes.right = 32767;        /* max width */
  77.     
  78.     newSize = GrowWindow(theWindow,evt->where,&growLimitSizes);
  79.     newHeight = HiWord(newSize);
  80.     newWidth = LoWord(newSize);
  81.     SizeWindow(theWindow,newWidth,newHeight,TRUE);
  82. }
  83.  
  84. DoZoom(register EventRecord *evt, register WindowPtr theWindow, int part)
  85. {
  86.     GrafPtr savePort;
  87.     
  88.     GetPort(&savePort);
  89.     SetPort(theWindow);
  90.     
  91.     if(TrackBox(theWindow,evt->where,part)) {
  92.         ZoomWindow(theWindow,part,true);
  93.     }
  94.     
  95.     SetPort(savePort);
  96. }
  97.  
  98.  
  99.  
  100. void DoMenu(register long msel)
  101. {
  102.     int item,menu;
  103.     item = LoWord(msel);
  104.     menu = HiWord(msel);
  105.     MenuDispatch(menu, item);
  106.     HiliteMenu(0);                        /* remove menu title hiliting */
  107. }
  108.  
  109. void DoKey(register EventRecord *evt)
  110. {
  111.     char        c;
  112.     
  113.     c = (char)evt->message & charCodeMask;
  114.     
  115.     if((evt->modifiers & cmdKey)== FALSE) {
  116.         KeyPressed = true;
  117.         KeyValue = c;
  118.     } else {
  119.         DoMenu(MenuKey(evt->message & charCodeMask));    
  120.     }
  121. }
  122.  
  123. void DrawClippedGrowIcon(WindowPtr theWindow)
  124. /*
  125.     Clip out the lines that appear
  126.     on the sides of a window with a grow icon.
  127. */
  128. {
  129.     Rect        clip;
  130.     RgnHandle    oldClip;
  131.     
  132.     oldClip = NewRgn();
  133.     GetClip(oldClip);
  134.     clip = theWindow->portRect;
  135.     clip.left = clip.right - 15;
  136.     clip.top = clip.bottom - 15;
  137.  
  138.     ClipRect(&clip);
  139.     
  140.     DrawGrowIcon(theWindow);
  141.     SetClip(oldClip);
  142. }
  143.  
  144. void DoUpdate(register EventRecord *evt)
  145. {
  146.     WindowPtr    updateWindow;
  147.     GrafPtr        savePort;
  148.     
  149.     
  150.     GetPort(&savePort);                        /* save current port */
  151.     
  152.     updateWindow=(WindowPtr)evt->message;    /* get windowPtr from event msg */
  153.     SetPort(updateWindow);
  154.     BeginUpdate(updateWindow);                        
  155.     EraseRect(&updateWindow->portRect);        /* erase content region */
  156.  
  157.     DrawImage(updateWindow);                /* sample routine to draw contents */
  158.     
  159.     DrawControls(updateWindow);                /* draw any controls in the window */
  160.     DrawClippedGrowIcon(updateWindow);
  161.     EndUpdate(updateWindow);
  162.         
  163.     SetPort(savePort);
  164. }
  165.  
  166.  
  167. void ActivateWindow(register WindowRecord    *newFrontWindow)
  168. {
  169.     /* This window is now active.  Controls should be enabled, etc. */
  170. }
  171.  
  172. void DeactivateWindow(register WindowRecord    *newBehindWindow)
  173. {
  174.     /* 
  175.         do anyting necessary to deactivate your windows here.
  176.         controls should be dimmed, etc.
  177.     */
  178. }
  179.  
  180. void DoActivate(register EventRecord *evt)
  181. {
  182.     if(evt->modifiers & activeFlag)
  183.         ActivateWindow((WindowRecord *)evt->message);
  184.     else
  185.         DeactivateWindow((WindowRecord *)evt->message);
  186. }
  187.  
  188. void DoMFinder(register EventRecord *evt)
  189. {
  190.     if( (evt->message >> 24) == suspendResumeMessage)
  191.         BackgroundFlag = !(evt->message & resumeFlag);
  192. }
  193.  
  194. void DoClick(register EventRecord *evt)
  195. {
  196.     WindowPtr    theWindow;
  197.     
  198.     switch(FindWindow(evt->where, &theWindow)) {
  199.         case inDesk:        break;
  200.         case inMenuBar:        DoMenu(MenuSelect(evt->where));
  201.                             break;
  202.         case inSysWindow:    SystemClick(evt,theWindow);
  203.                             break;
  204.         case inContent:        DoClickInContent(evt,theWindow);
  205.                             break;
  206.         case inDrag:        DoDragWindow(evt,theWindow);
  207.                             break;
  208.         case inGrow:        DoGrowWindow(evt,theWindow);
  209.                             break;
  210.         case inGoAway:        DoCloseWindow(evt,theWindow);
  211.                             break;
  212.         case inZoomIn:        DoZoom(evt,theWindow,inZoomIn);
  213.                             break;
  214.         case inZoomOut:        DoZoom(evt,theWindow,inZoomOut);
  215.                             break;
  216.         default:            break;
  217.     }
  218. }
  219.  
  220.  
  221. void MainEvent(void)
  222. {
  223.     EventRecord    event;
  224.     Boolean        eventOccured;
  225.     
  226.     KeyPressed = false;                /* set to false every time through    */
  227.     
  228.     if(WNE_available)
  229.         eventOccured = WaitNextEvent(everyEvent,&event,10,nil);
  230.     else {
  231.         SystemTask();
  232.         eventOccured = GetNextEvent(everyEvent, &event);
  233.     }
  234.  
  235.     if(eventOccured) {
  236.         switch(event.what) {
  237.             case nullEvent:                                        break;
  238.             case mouseDown:            DoClick(&event);            break;
  239.             case mouseUp:                                         break;
  240.             case keyDown:            DoKey(&event);                break;
  241.             case keyUp:                                             break;
  242.             case autoKey:            DoKey(&event);                break;
  243.             case updateEvt:            DoUpdate(&event);            break;
  244.             case diskEvt:                                         break;
  245.             case activateEvt:        DoActivate(&event);            break;
  246.             case networkEvt:                                    break;
  247.             case driverEvt:                                         break;
  248.             case app1Evt:                                         break;
  249.             case app2Evt:                                         break;
  250.             case app3Evt:                                         break;
  251.             case osEvt:                DoMFinder(&event);            break;
  252.             default:                                            break;
  253.         }
  254.     }
  255. }
  256.  
  257.  
  258.